package cn.patient.pp.module.fiveInOne.controller.admin;

import cn.patient.pp.framework.apilog.core.annotation.ApiAccessLog;
import cn.patient.pp.framework.common.pojo.CommonResult;
import cn.patient.pp.framework.common.pojo.PageParam;
import cn.patient.pp.framework.common.pojo.PageResult;
import cn.patient.pp.framework.common.util.object.BeanUtils;
import cn.patient.pp.framework.excel.core.listener.TemplateCheckListener;
import cn.patient.pp.framework.excel.core.util.ExcelUtils;
import cn.patient.pp.module.fiveInOne.controller.admin.vo.KeyPersonProtectionImportExcelVO;
import cn.patient.pp.module.fiveInOne.controller.admin.vo.KeyPersonProtectionPageReqVO;
import cn.patient.pp.module.fiveInOne.controller.admin.vo.KeyPersonProtectionRespVO;
import cn.patient.pp.module.fiveInOne.controller.admin.vo.KeyPersonProtectionSaveReqVO;
import cn.patient.pp.module.fiveInOne.dal.dataobject.KeyPersonProtectionDO;
import cn.patient.pp.module.fiveInOne.service.KeyPersonProtectionService;
import cn.patient.pp.module.patients.controller.admin.vo.importExecl.ImportRespVO;
import cn.patient.pp.module.patients.controller.admin.vo.importExecl.PatientsDOExeclVO;
import cn.patient.pp.module.patients.framework.web.config.Config;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import static cn.patient.pp.framework.apilog.core.enums.OperateTypeEnum.EXPORT;
import static cn.patient.pp.framework.apilog.core.enums.OperateTypeEnum.IMPORT;
import static cn.patient.pp.framework.common.pojo.CommonResult.success;

/**
 * @author liqingsong
 * @version 1.0
 * Create by 2025/6/11
 */

@Tag(name = "管理后台 - 重点人员包保信息")
@RestController
@RequestMapping("/business/key-person-protection")
@Validated
public class FiveInOneController {

    @Resource
    private KeyPersonProtectionService keyPersonProtectionService;

    @PostMapping("/create")
    @Operation(summary = "创建重点人员包保信息")
    @PreAuthorize("@ss.hasPermission('business:key-person-protection:create')")
    public CommonResult<Integer> createKeyPersonProtection(@Valid @RequestBody KeyPersonProtectionSaveReqVO createReqVO) {
        return success(keyPersonProtectionService.createKeyPersonProtection(createReqVO));
    }

    @PostMapping("/update")
    @Operation(summary = "更新重点人员包保信息")
    @PreAuthorize("@ss.hasPermission('business:key-person-protection:update')")
    public CommonResult<Boolean> updateKeyPersonProtection(@Valid @RequestBody KeyPersonProtectionSaveReqVO updateReqVO) {
        keyPersonProtectionService.updateKeyPersonProtection(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除重点人员包保信息")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('business:key-person-protection:delete')")
    public CommonResult<Boolean> deleteKeyPersonProtection(@RequestParam("id") Integer id) {
        keyPersonProtectionService.deleteKeyPersonProtection(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得重点人员包保信息")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('business:key-person-protection:query')")
    public CommonResult<KeyPersonProtectionRespVO> getKeyPersonProtection(@RequestParam("id") Integer id) {
        KeyPersonProtectionRespVO keyPersonProtection = keyPersonProtectionService.getKeyPersonProtection(id);
        return success(keyPersonProtection);
    }

    @PostMapping("/page")
    @Operation(summary = "获得重点人员包保信息分页")
    @PreAuthorize("@ss.hasPermission('business:key-person-protection:query')")
    public CommonResult<PageResult<KeyPersonProtectionRespVO>> getKeyPersonProtectionPage(@Valid @RequestBody KeyPersonProtectionPageReqVO pageReqVO) {
        PageResult<KeyPersonProtectionDO> pageResult = keyPersonProtectionService.getKeyPersonProtectionPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, KeyPersonProtectionRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出重点人员包保信息 Excel")
    @PreAuthorize("@ss.hasPermission('business:key-person-protection:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportKeyPersonProtectionExcel(@Valid KeyPersonProtectionPageReqVO pageReqVO,
                                               HttpServletResponse response) throws IOException {
        ArrayList<PatientsDOExeclVO> list = new ArrayList<>();
        // 导出 Excel
        ExcelUtils.write(response, "五帮一导入模板.xls", "数据", KeyPersonProtectionImportExcelVO.class,
                BeanUtils.toBean(list, KeyPersonProtectionImportExcelVO.class));
    }

    @PostMapping("/import-keypersonprotection")
    @Operation(summary = "导入五帮一数据")
    @ApiAccessLog(operateType = IMPORT)
    @Parameters({
            @Parameter(name = "file", description = "Excel 文件", required = true),
            @Parameter(name = "updateSupport", description = "是否支持更新，默认为 false", example = "true")
    })
    @PreAuthorize("@ss.hasPermission('business:key-person-protection:import')")
    public CommonResult<ImportRespVO> importExcel(@RequestParam("file") MultipartFile file,
                                                  @RequestParam(value = "updateSupport", required = false, defaultValue = "false") Boolean updateSupport) throws Exception {
        TemplateCheckListener<KeyPersonProtectionImportExcelVO> listener = new TemplateCheckListener<>(Config.KEY_PERSON_PROTECTION_INFO);
        List<KeyPersonProtectionImportExcelVO> list = ExcelUtils.read(file, KeyPersonProtectionImportExcelVO.class, listener);
        return success(keyPersonProtectionService.importKeyPersonProtectionList(list, updateSupport));
    }

}
